package com.mw.mwreaderdemo;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class AT1604 extends Activity {

	private Button btnReadErrCounter;
	private Button btnCheckPwd;
	private Button btnReadPwd;
	private Button btnChangePwd;
	private Button btnReadData;
	private Button btnWriteData;
	private Button btnSP;
	private Button btnCancelSP;
	private Button btnPersonalized;
	private Button btnReadProtClear;
	private Button btnWriteProtClear;

	private Spinner spPwdPos;
	private Spinner spPwdType;
	private Spinner spApplication;

	private EditText etPassword;
	private EditText etData;
	private EditText etOffset;
	private EditText etLen;
	private EditText etResult;

	//  上一次点击的时间 long型  
	private long beforeClick = 0;
	private String TAG="AT1604";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_at1604);

		btnReadErrCounter = (Button) findViewById(R.id.btnReadErrCounter);
		btnCheckPwd = (Button) findViewById(R.id.btnCheckPwd);
		btnReadPwd = (Button) findViewById(R.id.btnReadPwd);
		btnChangePwd = (Button) findViewById(R.id.btnChangePwd);
		btnReadData = (Button) findViewById(R.id.btnReadData);
		btnWriteData = (Button) findViewById(R.id.btnWriteData);
		btnSP = (Button) findViewById(R.id.btnSP);
		btnCancelSP = (Button) findViewById(R.id.btnCancelSP);
		btnPersonalized = (Button) findViewById(R.id.btnPersonalized);
		btnReadProtClear = (Button) findViewById(R.id.btnReadProtClear);
		btnWriteProtClear = (Button) findViewById(R.id.btnWriteProtClear);

		spPwdPos = (Spinner) findViewById(R.id.spPwdPos);
		spPwdType = (Spinner) findViewById(R.id.spPwdType);
		spApplication = (Spinner) findViewById(R.id.spApplication);

		etPassword = (EditText) findViewById(R.id.etPassword);
		etData = (EditText) findViewById(R.id.etData);
		etOffset = (EditText) findViewById(R.id.etOffset);
		etLen = (EditText) findViewById(R.id.etLen);
		etResult = (EditText) findViewById(R.id.etResult);

		// 设置输入字符转换为大写
		etPassword.setTransformationMethod(new InputLowerToUpper());
		etData.setTransformationMethod(new InputLowerToUpper());

		// 设置双击操作信息提示框清除数据
		etResult.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (System.currentTimeMillis() - beforeClick > 500) {
						beforeClick = System.currentTimeMillis();
					} else {
						etResult.setText("");
					}
				}
				return false;
			}
		});

		spPwdPos.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if (position == 0) {
					spPwdType.setVisibility(View.INVISIBLE);
				} else {
					spPwdType.setVisibility(View.VISIBLE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
	
		btnReadErrCounter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				int sector = spPwdPos.getSelectedItemPosition();
				int type = spPwdType.getSelectedItemPosition();
				Log.i(TAG, String.valueOf(sector) +" | "+ String.valueOf(type));
				try {
					int counter = MainActivity.reader.readCount1604(sector, type);
					etResultAddStr("所选密码错误计数为: " + counter);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
	
		btnCheckPwd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				int sector = spPwdPos.getSelectedItemPosition();
				int type = spPwdType.getSelectedItemPosition();
				Log.i(TAG, String.valueOf(sector) +" | "+ String.valueOf(type));
				try {
					int it = MainActivity.reader.verifyPassword1604(sector, type, etPassword.getText().toString());
					etResultAddStr("校验密码成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
	
		btnReadPwd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				int sector = spPwdPos.getSelectedItemPosition();
				int type = spPwdType.getSelectedItemPosition();
				Log.i(TAG, String.valueOf(sector) +" | "+ String.valueOf(type));
				try {
					etResultAddStr(MainActivity.reader.readPassword1604(sector, type));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
	
		btnChangePwd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				int sector = spPwdPos.getSelectedItemPosition();
				int type = spPwdType.getSelectedItemPosition();
				Log.i(TAG, String.valueOf(sector) +" | "+ String.valueOf(type));
				try {
					MainActivity.reader.changePassword1604(sector, type, etPassword.getText().toString());
					etResultAddStr("更改密码成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
	
		btnReadData.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				int offset = Integer.parseInt(etOffset.getText().toString());
				int length = Integer.parseInt(etLen.getText().toString());
				try {
					etResultAddStr(MainActivity.reader.read1604(offset, length));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnWriteData.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				int offset = Integer.parseInt(etOffset.getText().toString());
				String str = etData.getText().toString();
				int len = str.length() / 2;
				try {
					//先擦除
					MainActivity.reader.erase1604(offset, len);
					MainActivity.reader.write1604(offset, str);
					etResultAddStr("写数据成功!");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnSP.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				try {
					MainActivity.reader.simulationPersonalization1604(1);
					etResultAddStr("模拟个人化成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
		
		btnCancelSP.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				try {
					MainActivity.reader.simulationPersonalization1604(0);
					etResultAddStr("取消模拟个人化成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnPersonalized.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				try {
					MainActivity.reader.personalization1604();
					etResultAddStr("个人化成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});

		btnReadProtClear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				try {
					int sector = spApplication.getSelectedItemPosition() + 1;
					MainActivity.reader.initReadControl1604(sector);
					etResultAddStr("读保护位清0成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
		
		btnWriteProtClear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (MainActivity.reader == null) {
					Toast.makeText(AT1604.this, "设备未打开", Toast.LENGTH_SHORT).show();
					return;
				}
				
				try {
					int sector = spApplication.getSelectedItemPosition() + 1;
					MainActivity.reader.initWriteControl1604(sector);
					etResultAddStr("写保护位清0成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					etResultAddStr(e.getMessage());
				}
			}
		});
		
	}

	/**
	 * @Description 提示信息框添加信息
	 * @param str
	 *            要添加的信息
	 * @return void
	 */
	private void etResultAddStr(String str) {
		etResult.setText(etResult.getText() + " " + str + "\n");
		etResult.setSelection(etResult.length());// 调整光标到最后一行
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.at1604, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
